package com.cf.r1034;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class f {

    public static List<int[]> list;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        in.nextToken();
        int t = (int) in.nval;
        while (t-- > 0) {
            in.nextToken(); int n = (int) in.nval;
            int[] a = new int[n + 1];
            for (int i = 0; i < n; i++) {
                a[i] = i + 1;
            }
            list = new ArrayList<>();
            dfs(a, 0);
            list.sort(Comparator.comparingInt(x -> x[x.length - 1]));
            int[] ans = list.get(0);
            for (int i = 0; i < n; i++) {
                out.print(ans[i] + " ");
            }
            out.println();
        }
        out.flush();
        out.close();
        br.close();
    }

    public static void dfs(int[] arr, int i) {
        if (i == arr.length) {
            for (int j = 1; j < arr.length; j++) {
                if (gcd(arr[j], j + 1) <= 1) {
                    return;
                }
            }
            int[] tmp = new int[arr.length];
            int x = 0;
            for (int j = 0; j < tmp.length - 1; j++) {
                tmp[j] = arr[j];
                if (tmp[j] == j + 1) {
                    x++;
                }
            }
            tmp[tmp.length - 1] = x;
            list.add(tmp);
            return;
        }
        for (int j = i; j < arr.length; j++) {
            swap(arr, i, j);
            dfs(arr, i + 1);
            swap(arr, i, j);
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}